package cn.jwm.login.service.imp;

import cn.jwm.constant.LoginConstant;
import cn.jwm.login.dto.LoginDTO;
import cn.jwm.login.service.loginService;
import cn.jwm.org.domain.Employee;
import cn.jwm.org.service.IEmployeeService;
import cn.jwm.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;
import java.util.UUID;

@Service
public class loginServiceImpl implements loginService {
    @Autowired
    private IEmployeeService iEmployeeService;
    @Override
    public AjaxResult login(LoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        if (StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            throw new RuntimeException("用户名或密码不能为空!请检查后重新提交!");
        }
        Employee employee = iEmployeeService.getByUsername(username);
        if (Objects.isNull(employee)){
            throw new RuntimeException("用户名或密码错误!请检查后重新提交!");
        }
        if(!password.equals(employee.getPassword())){
            throw new RuntimeException("用户名或密码错误!请检查后重新提交!");
        }
        String token = UUID.randomUUID().toString();


        LoginConstant.loginMap.put(token, employee);

        // 6 携带token返回前端
        return AjaxResult.me().setDataObj(token);

    }
}
